<?php

/**
 * This is the model class for table "order".
 *
 * The followings are the available columns in table 'order':
 * @property integer $id ID
 */
class Order extends ActiveRecord
{
	
	const PAY_TYPE_ALIPAY = 'alipay';
	const PAY_TYPE_TENPAY = 'tenpay';
	const PAY_TYPE_OFFLINE = 'offline';
	public $providerId;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Order the static model class
	 */
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	
	public function getTotalPrice() {
		return '￥'.$this->total;
	}
	
	public function getMdcClass() {
		return 'application.models.metadata.OrderMetadata';
	}
	
	public static function createOrder($objectType, $data=array()) {
		$model = NULL;
		switch ($objectType) {
			case Goods::TYPE_HOTEL:
				$model = new OrderHotel();
				break;
			case Goods::TYPE_TRAVEL:
				$model = new OrderTravel();
				break;
			case Goods::TYPE_SCENIC:
				$model = new OrderScenic();
				break;
			case Goods::TYPE_NATIVE:
				$model = new OrderNative();
				break;
				case Goods::TYPE_TICKET:
				$model = new OrderTicket();
				break;
			default:
				break;
		}
		if($model)
			$model->attributes = $data;
		return $model;
	}
	
	public function getPayStatus() {
		if (is_object($this->pay)) {
			return CHtml::value($this, 'pay.status') ? '已支付' : '未支付';
		} else 
			return '未支付';
	}
	
	/**
	 * 是否可以付款
	 * Enter description here ...
	 */
	public function isPayAble() {
		return $this->status != 'ST_CANCELED' && $this->pay_type != self::PAY_TYPE_OFFLINE && CHtml::value($this, 'pay.status') != 1;
	}
	
	/**
	 * 是否可以取消
	 * Enter description here ...
	 */
	public function isCanceldAble() {
		return (!is_object($this->pay) && $this->status != 'ST_CANCELED') || $this->status == 'ST_NONE';
	}
	
	/**
	 * 是否可以点评
	 * Enter description here ...
	 */
	public function isCommentAble() {
		$type = get_class(CHtml::value($this, 'items.goods'));
		$comment = GoodsComment::model()->findByAttributes(array(
			'target_object_type'=>$type,
			'target_object_id'=>CHtml::value($this, 'items.goods_id'),
			'uid'=>Yii::app()->adminUser->id,
			'order_id' => $this->id,
		));
		
		return $this->status == 'ST_FINISHED' && !is_object($comment);
	}
}